package com.enjoyor.soa.traffic.core.tsms.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.enjoyor.soa.traffic.core.tsms.domain.TaskExecuteRecord;
import com.enjoyor.soa.traffic.core.tsms.mapper.ITaskExecuteRecordMapper;
import com.enjoyor.soa.traffic.core.tsms.service.ITaskExecuteRecordService;
import com.enjoyor.soa.traffic.frame.logger.IPubLogMapper;
import com.enjoyor.soa.traffic.util.enums.log.LogInfoDic;
import com.enjoyor.soa.traffic.util.helper.TimeHelper;
import com.enjoyor.soa.traffic.util.pojo.Page;
@Service
@Transactional
@SuppressWarnings("rawtypes")
public class TaskExecuteRecordService implements ITaskExecuteRecordService{

	@Autowired
	private ITaskExecuteRecordMapper iTaskExecuteRecordMapper;
	
	@Autowired
	private IPubLogMapper iPubLogMapper;
	
	@Override
	public TaskExecuteRecord getExecuteTaskRecordById(String recordId) {
		TaskExecuteRecord taskExecuteRecord = new TaskExecuteRecord();
		taskExecuteRecord = iTaskExecuteRecordMapper.selectById(recordId);
		return taskExecuteRecord;
	}
	@Override
	public List<TaskExecuteRecord> getExecuteTaskRecordList(String recordId, String executeTaskId, String taskId, 
			String recordTypeId, String isWarn, String beginTime, String endTime){
		// TODO Auto-generated method stub
		String[] executeTaskIdList = new String[]{};
		if(executeTaskId != null && !"".equals(executeTaskId)) {
			executeTaskIdList = executeTaskId.split(",");
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("recordId", recordId);
		//map.put("executeTaskId", executeTaskId);
		map.put("executeTaskIdList", executeTaskIdList);
		map.put("taskId", taskId);
		map.put("recordTypeId", recordTypeId);
		map.put("isWarn", isWarn);
		map.put("beginTime", beginTime);
		map.put("endTime", endTime);
		return iTaskExecuteRecordMapper.getExecuteTaskRecordList(map);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public Page getExecuteTaskRecordPage(Page page, String recordId, String executeTaskId, String taskId, 
			String recordTypeId, String isWarn, String beginTime, String endTime) {
		// TODO Auto-generated method stub
		String[] executeTaskIdList = new String[]{};
		if(executeTaskId != null && !"".equals(executeTaskId)) {
			executeTaskIdList = executeTaskId.split(",");
		}
    	Map<String,Object> mapBean = new HashMap<String,Object>();
    	mapBean.put("recordId", recordId);
		//mapBean.put("executeTaskId", executeTaskId);
    	mapBean.put("executeTaskIdList", executeTaskIdList);
    	mapBean.put("taskId", taskId);
    	mapBean.put("recordTypeId", recordTypeId);
    	mapBean.put("isWarn", isWarn);
    	mapBean.put("beginTime", beginTime);
    	mapBean.put("endTime", endTime);
    	page.setMapBean(mapBean);
        return iTaskExecuteRecordMapper.selectPageInfo(page);
    }
	
	@Override
    public String insertRecord(TaskExecuteRecord taskExecuteRecord, String personName) {
		// TODO Auto-generated method stub
		String recordId = "TR"+TimeHelper.getUniqueString();
		taskExecuteRecord.setRecordId(recordId);
		taskExecuteRecord.setRecordTime(TimeHelper.dateToLongString());
		iTaskExecuteRecordMapper.insert(taskExecuteRecord);
		iPubLogMapper.saveNoramlLogInfo(personName, null,LogInfoDic.LOG_TRANSTYPE_ENUM.TRANSTYPE_INSERT.getCode(),
                String.format("添加执行任务记录信息:{%s} 操作成功!", new Object[] { taskExecuteRecord.getId() }),
                LogInfoDic.LOG_MODULAR_ENUM.TSMS_TASK_INFO_MANAGEMENT.getMessage());
		return recordId;
	}
}
